package com.edu.zut.controller;


import com.edu.zut.domain.CheckVo;
import com.edu.zut.result.ResponseResult;
import com.edu.zut.service.ResultService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 许文科
 * @since 2023-02-20
 */
@RestController
@RequestMapping("/api/result")
public class ResultController {

    @Resource
    private ResultService resultService;


    @GetMapping("/list")
    @PreAuthorize("hasAnyRole('admin','teacher','student')")
    public ResponseResult getResultList() {
        //获取登录者的用户名
        String username = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return resultService.getResultList(username);
    }

    @DeleteMapping("/delete")
    @PreAuthorize("hasAnyRole('admin','teacher','student')")
    public ResponseResult delete(@RequestParam("resultId") Long resultId) {
        return resultService.delete(resultId);
    }

    @PostMapping("/check")
    @PreAuthorize("hasAnyRole('admin','teacher','student')")
    public ResponseResult check(@RequestBody CheckVo checkVo) {
        return resultService.check(checkVo);
    }

    @GetMapping("/find")
    @PreAuthorize("hasAnyRole('admin','teacher')")
    public ResponseResult find(@RequestParam("username") String userName) {
        return resultService.find(userName);
    }
}
